<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="请选择分类"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option label="BUG提交" value="1"></el-option>
          <el-option label="系统建议" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="等级" prop="level">
        <el-select
          v-model="queryParams.level"
          placeholder="请选择等级"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option label="一般" value="1"></el-option>
          <el-option label="重要" value="2"></el-option>
          <el-option label="严重" value="3"></el-option>
          <el-option label="重大事故" value="4"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="处理人" prop="handler">
        <el-input
          v-model="queryParams.handler"
          placeholder="请输入处理人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <el-option label="未处理" value="1"></el-option>
          <el-option label="已处理" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-thumb"
          size="mini"
          @click="responseUpdate"
          v-hasPermi="['bugHandle:bugHandle:add']"
        >批量处理
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bugHandle:bugHandle:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bugHandle:bugHandle:remove']"
        >批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bugHandle:bugHandle:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bugHandleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="标题" align="center" prop="title" show-overflow-tooltip/>
      <el-table-column label="分类" align="center" prop="category" :formatter="categoryFormat"/>
      <el-table-column label="等级" align="center" prop="level" :formatter="levelFormat"/>
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="创建用户" align="center" prop="createBy"/>
      <el-table-column label="处理时间" align="center" prop="updateTime"/>
      <el-table-column label="处理人" align="center" prop="handler"/>
      <el-table-column label="处理状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="responseUpdate(scope.row)"
            v-hasPermi="['bugHandle:bugHandle:edit']"
          >处理
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['bugHandle:bugHandle:remove']"
          >详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改BUG提交对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" align="center" append-to-body>
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
               label-position="top">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" :maxlength="20" show-word-limit
                    clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-select v-model="form.category" placeholder="请选择分类">
            <el-option label="BUG提交" :value="1"></el-option>
            <el-option label="系统建议" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-tooltip class="item" effect="dark" placement="bottom">
          <div slot="content">运维将会在30分钟内响应您的请求
            <br/>一般:24小时内解决
            <br/>重要:8小时内解决
            <br/>严重:4小时内解决
            <br/>重大事故:开发团队将立即开会协商解决
          </div>
          <span>
                 <el-form-item label="紧急等级" prop="level">
            <el-select v-model="form.level" placeholder="请选择等级">
              <el-option label="一般" :value="1"></el-option>
              <el-option label="重要" :value="2"></el-option>
              <el-option label="严重" :value="3"></el-option>
              <el-option label="重大事故" :value="4"></el-option>
            </el-select>
          </el-form-item>
           </span>
        </el-tooltip>
        <el-form-item label="图片">
          <image-upload v-model="form.image"/>
        </el-form-item>

        <el-form-item label="详情描述" prop="desc">
          <el-input v-model="form.description" type="textarea" placeholder="请输入详情描述" :maxlength="200"
                    show-word-limit :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="提交详情" :visible.sync="details" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-col :span="24">
          <el-form-item label="标题：">{{ form.title }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="详情描述：">{{ form.description }}</el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="details = false">关 闭</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listBugHandle,
  getBugHandle,
  delBugHandle,
  addBugHandle,
  updateBugHandle,
  resBugHandle
} from "@/api/bugHandle/bugHandle";

export default {
  name: "BugHandle",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BUG提交表格数据
      bugHandleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      details: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        category: null,
        level: null,
        image: null,
        description: null,
        handler: null
      },
      // 表单参数
      form: {},
      //level转化

      map: {
        boolean: {
          1: '否',
          2: '是',
          3: '不知道',
          4: '不知道',
        }
      },


      // 表单校验
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
        category: [
          {required: true, message: '请选择分类', trigger: 'change'}
        ],
        level: [
          {required: true, message: '请选择等级', trigger: 'change'}
        ],

        desc: [
          {required: true, message: '请填写请求详情', trigger: 'blur'},
          {min: 0, max: 200, message: '长度在 0 到 200 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleView(row) {
      this.details = true;
      this.form = row;
    },
    levelFormat(row) {
      if (row.level === 1) {
        return '一般'
      } else if (row.level === 2) {
        return '重要'
      } else if (row.level === 3) {
        return '严重'
      } else if (row.level === 4) {
        return '重大事故'
      }
    },
    categoryFormat(row) {
      if (row.category === 1) {
        return 'BUG提交'
      } else if (row.category === 2) {
        return '系统建议'
      }
    },
    statusFormat(row) {
      if (row.status === 1) {
        return '待处理'
      } else if (row.status === 2) {
        return '已处理'
      }
    },

    /** 查询BUG提交列表 */
    getList() {
      this.loading = true;
      listBugHandle(this.queryParams).then(response => {
        this.bugHandleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        category: null,
        level: null,
        image: null,
        description: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        handler: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加BUG提交";
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBugHandle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改BUG提交";
      });
    },
    /** 响应按钮操作 */
    responseUpdate(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认处理BUG提交编号为"' + ids + '"的数据项？').then(function () {
        return resBugHandle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("批量处理成功");
      }).catch(() => {
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBugHandle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBugHandle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除BUG提交编号为"' + ids + '"的数据项？').then(function () {
        return delBugHandle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bugHandle/bugHandle/export', {
        ...this.queryParams
      }, `bugHandle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.el-tooltip__popper {
  max-width: 30%;
}
</style>
